import request from "./request";

//记录学习时长
export default class RecordStudyTime {
  timer = null;
  checkType = "";
  constructor(checkType) {
    this.checkType = checkType;
    this.startRecordStudyTime();
  }
  //向服务器发送请求以记录学习时长
  sendStudyTime = () => {
    request({
      url: "userCheckPlan/endOnlineCheckIn",
      data: {
        checkType: this.checkType,
      },
    });
  };
  //开始记录学习时长（仅开始的时候调用一次）
  startRecord = () => {
    request({
      url: "userCheckPlan/startOnlineCheckIn",
      data: {
        checkType: this.checkType,
      },
    });
  };
  //每隔1分钟向服务器请求以记录用户的学习时长
  startRecordStudyTime = () => {
    this.startRecord();
    this.timer = setInterval(
      () => {
        this.sendStudyTime();
      },
      1000 * 60 + 5,
    );
  };
  //停止记录学习时长
  stopRecordStudyTime = () => {
    this.sendStudyTime();
    clearInterval(this.timer);
  };
}
